package com.epcs.model.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.epcs.model.dto.Account;
import com.epcs.model.dto.Setting;
import com.epcs.util.DBUtil;

public class SettingDAO {

	public boolean updateSetting(Setting setting) {

		Session session = DBUtil.getSession();
		Transaction trans = null;
		try {
			trans = session.beginTransaction();
			session.update(setting);
			trans.commit();
			
			return true;
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
			trans.rollback();
		} 
		finally {
			if (session != null) {
				session.close();
			}
		}

		return false;
	}

	public Setting getSettingFromDB(Account account) {

		Session session = DBUtil.getSession();
		try {
			Criteria criteria = session.createCriteria(Setting.class);
			Criterion criterion = Restrictions.eq("account", account);
			criteria.add(criterion);
			Setting item = (Setting) criteria.uniqueResult();
			return item;
		} catch (HibernateException e) {
			System.out.println(e.getMessage());		} 
		finally {
			if (session != null) {
				session.close();
			}
		}

		return null;
	}

}
